package com.zhongketech.sunflower.activity;

import java.io.Serializable;
import java.util.List;

import com.baidu.platform.comapi.map.E;
import com.zhongketech.sunflower.Constant;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.adapter.TimebucketInfoAdapter;
import com.zhongketech.sunflower.sv.SvManager;
import com.zhongketech.sunflower.transport.model.Supervision;
import com.zhongketech.sunflower.transport.model.Times;
import com.zhongketech.sunflower.widget.ListViewMeasure;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class TimeBucketInfoActivity extends BaseActivity implements View.OnClickListener {
	
	private ListViewMeasure mControlListView;
	private TimebucketInfoAdapter mTimeAdapter;
	private Supervision mTimeSupervision;
	private TextView supervisionTitle;
	private TextView controlDateText;
	private TextView controlItemsText;
	private TextView priorityText;
	private TextView switchText;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_timebucket_info);
		
		getSupervision(getIntent().getStringExtra("id"));  
    
		initView();
		mTimeAdapter = new TimebucketInfoAdapter(this, mTimeSupervision.getTime_segments());
		mControlListView.setAdapter(mTimeAdapter);
	}
	
	private void getSupervision(String id)
	{
		if(null != id && null != SvManager.getInstance().getMySupervision() && null != SvManager.getInstance().getMySupervision().getSupervisons())
		{	
			List<Supervision> list= SvManager.getInstance().getMySupervision().getSupervisons();
			for(int i = 0;i < list.size();i ++)
			{
				if(id.equals(list.get(i).getId()))
				{
					mTimeSupervision = list.get(i);
				}
			}
		}
		else
		{
			MyToast.getInstance().show("没有中找到对应策略:"+id);
		}
	}

	private void initView() {
		if("one_key".equals(mTimeSupervision.getType()))
		{
			((TextView) findViewById(R.id.title)).setText("一键控制");	
		}
		else
		{
			((TextView) findViewById(R.id.title)).setText("时间控制");
		}
		
		//		((TextView) view).setText(getString(R.string.timebucket_control));
		//		view = findViewById(R.id.other);
		//		view.setVisibility(View.VISIBLE);
		//		view.setOnClickListener(this);
		//		((TextView) view).setText(getString(R.string.edit));
		//		view = findViewById(R.id.back);
		//		view.setOnClickListener(this);
		mControlListView = (ListViewMeasure) findViewById(R.id.timectrl_list);
		supervisionTitle = (TextView) findViewById(R.id.supervisionTitle);
		controlDateText = (TextView) findViewById(R.id.controlDateText);
		controlItemsText = (TextView) findViewById(R.id.controlItemsText);
		priorityText = (TextView) findViewById(R.id.priorityText);
		switchText = (TextView) findViewById(R.id.switchText);

	}

	public void onResume() {
		super.onResume();
		supervisionTitle.setText(mTimeSupervision.getTitle());
		controlDateText.setText(getWeek(mTimeSupervision.getPeriod()));
		controlItemsText.setText(getControlText());

		if (mTimeSupervision.getPriority() == 0) {
			priorityText.setText("高");
		} else if (mTimeSupervision.getPriority() == 1) {
			priorityText.setText("中");
		} else {
			priorityText.setText("低");
		}

		if (mTimeSupervision.getEnable()) {
			switchText.setText("开");
		} else {
			switchText.setText("关");
		}

	}
	
	private String getControlText()
	{
		String text = "";
		if(mTimeSupervision.getCall().getEnable())
		{
			text = text+" 电话";
		}
		if(mTimeSupervision.getSms().getEnable())
		{
			text = text+" 短信";
		}
		if(mTimeSupervision.getScreen_lock().getEnable())
		{
			text = text+" 锁屏";
		}
		if(mTimeSupervision.getNetwork().getEnable())
		{
			text = text+" 断网";
		}
		if(mTimeSupervision.getEyes_protect().getEnable())
		{
			text = text+" 眼睛保护";
		}
		if(!Constant.APP_ENABLE_ALL.equals(mTimeSupervision.getApplication().getEnable()))
		{
			text = text+" 程序";
		}
		
		return text;
	}

	private static final String[] weekArray = {"周一 ",
		"周二 ",
		"周三 ",
		"周四 ",
		"周五 ",
		"周六 ",
		"周日"};
	private String getWeek(int[] array)
	{
		String week = "";
		for(int i = 0;null != array && i < array.length;i ++)
		{
			week = week + weekArray[array[i] - 1];
		}

		return week;
	}

//	private String getTime(List<Times> list)
//	{
//		String time = "";
//		for(int i = 0;null != list && i < list.size();i ++)
//		{
//			String start = list.get(i).getStart_at() / 60+":"+(list.get(i).getStart_at() % 60);
//			String end = list.get(i).getEnd_at() / 60+":"+(list.get(i).getEnd_at() % 60);
//			time = time + start+"-"+end;
//			if(i < list.size() -1)
//			{
//				time = time + "、";
//			}
//		}
//		return time;
//	}

	//	public void onLowMemory() {
	//		super.onLowMemory();
	//		finish();
	//	}
	//
	//	public void onDestroy() {
	//		super.onDestroy();
	//	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		//		case R.id.back: // 返回
		//			finish();
		//			break;
		//		case R.id.other: // 编辑
		//			Intent intent = new Intent();
		//			intent.putExtra("mSupervision", (Serializable) mTimeSupervision);
		//			intent.putExtra(Constant.ITEM_ID, getIntent().getStringExtra(Constant.ITEM_ID));
		//			intent.putExtra(Constant.START, "TimeBucketInfoActivity");
		//			intent.setClass(this, AddTimeBucketActivity.class);
		//			startActivity(intent);
		//			break;
		//		default:
		//			break;
		//		}
		}
	}
}
